package org.example.hansp.collection_.map_;

import java.util.*;

@SuppressWarnings({"all"})
public class MapMethod {
    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.put("key3", "value3");
        map.put("key4", "value4");
        map.put("key5", "value5");
        map.put("key5", "value6");
        System.out.println(map);
        //根据键值删除映射关系
        System.out.println("============================");
        map.remove("key1");
        System.out.println(map);

//        根据键获取值
        Object value5 = map.get("key5");
        System.out.println(value5);

//        获取元素个数
        System.out.println(map.size());
//        判断个数是否为0
        System.out.println(map.isEmpty());

//      查找键是否存在
        System.out.println("结果="+map.containsKey("key2"));
//查找值是否存在
        System.out.println("结果="+map.containsValue("value2"));
//        clear:清空k-v
        map.clear();
        System.out.println(map);
//遍历的六种方式
//        获取所有键值
        Set set=map.keySet();
        Iterator iterator=set.iterator();
        while(iterator.hasNext()){
            String key=(String)iterator.next();
            String value=(String)map.get(key);
            System.out.println("key="+key+",value="+value);
        }
        for (Object key : set) {
            System.out.println(key+" "+map.get(key));
        }

//获取所有value值
        Collection values = map.values();
        for (Object value : values) {
            System.out.println(value);
        }
Iterator iterator1 = values.iterator();
        while (iterator1.hasNext()) {
            Object next = iterator1.next();
            System.out.println(next);
        }
//使用entrySet
        Set entrySet1 = map.entrySet();
        for (Object entry : entrySet1) {
            Map.Entry entry1 = (Map.Entry) entry;
            System.out.println(entry1.getKey()+"="+entry1.getValue());
        }
        Iterator iterator2 = entrySet1.iterator();
        while (iterator2.hasNext()) {
            Map.Entry entry2 = (Map.Entry) iterator2.next();
            System.out.println(entry2.getKey()+"="+entry2.getValue());
        }

    }
}
